#include "pagridplot.h"

logFinfo* LD_readInfo(int argc,char* argv[]){
  char buffer[PLIB_STR];
  char dummy;
  FILE* logfp;
  char logfile[PLIB_STR];

  logFinfo* info;

  /* alloc */
  info           = U_malloc(1,logFinfo,"LD_readinfo error 1.0\n");
  info->base     = U_malloc(PLIB_STR,char,"LD_readinfo error 1.1\n");
  if(argc == 3)
    info->coords = U_malloc(PLIB_STR,char,"LD_readinfo error 1.2\n");
  else
    info->coords = NULL;
  info->F_graph  = U_malloc(PLIB_STR,char,"LD_readinfo error 1.3\n");
  info->F_grid   = U_malloc(PLIB_STR,char,"LD_readinfo error 1.4\n");

  /* get graph file from log first line */
  strcpy(info->base,argv[1]);
  if(argc == 3)
    strcpy(info->coords,argv[2]);

  sprintf(logfile,"%s.log",argv[1]);
  WARNING(((logfp = fopen(logfile,"r")) == NULL,"LD_readinfo error 2 : cannot open %s\n",logfile),NULL);

  WARNING((fgets(buffer,PLIB_STR,logfp) == NULL,
	   "LD_readinfo error 2 : cannot read line 1\n"),NULL);  
  sscanf(buffer,"%c%s",&dummy,info->F_graph);  
  WARNING((fgets(buffer,PLIB_STR,logfp) == NULL,
	   "LD_readinfo error 3 : cannot read line 2\n"),NULL);  
  sscanf(buffer,"%c%s",&dummy,info->F_grid);

  fclose(logfp);

  return info;
}

void LD_freeInfo(logFinfo* info){
  U_free("LD_freeInfo error 1",&(info->base),&(info->coords),&(info->F_graph),&(info->F_grid),&info,LEND);
}


logFdata* LD_readLog(FILE *fp, logFctrl* ctrl){
  int i;
  logFdata *data;
  int pos[8],nbpos=8,nbread=10,beforelist=5,nblist=2;
  
  /* read and do stats */
  pos[LOG_CUTEDGE]              = 3;
  pos[LOG_TOTCOMMCOST]          = 4;
  pos[LOG_MAXWEIGHT]            = -1; /* max first processor series */
  pos[LOG_CUTEDGEBYTOTCOMMCOST] = 5;
  pos[LOG_IMBALANCE]            = 6;
  pos[LOG_MAXEXECTIME]          = 7;
  pos[LOG_TOTEXECTIME]          = 8;
  pos[LOG_IMBEXECTIME]          = 9;  
  
  WARNING(((data = LD_initLog(nbpos)) == NULL,"LD_readLog error code 1 : error in LD_initLog\n"),NULL);
  
  WARNING((LD_read(fp,log,pos,nbpos,nbread,nblist,beforelist) == 0,"LD_readLog error code 2 : error in LD_read\n"),NULL);    

  return log;
}

logFdata* LD_initLog(nbpos){
  logFdata = U_malloc(1,logFdata,"LD_initLog error 1\n");
  
  logFdata->max_i = SAMPLES;
  logFdata->i     = 0;

  logFdata->lvl   = U_malloc
}
